/*
 * lib2Desa - Another 2D game library in C++
 * Copyright (C) 2009 Erivaldo Xavier de Lima Filho
 *
 * This program is free software and is distributed under GNU General Public License version 3. You can modify and/or redistribute
 * it under the terms of GPL version 3 or posterior version. This program comes with NO WARRANTY, for more details, see LICENSE,
 * or <http://www.gnu.org/licenses>.
 *
 * Contact info:
 *
 *	Erivaldo Xavier de Lima Filho, desadoc@gmail.com
 *
 */

#include "Core/AEventNotifier.hpp"

using namespace std;

#ifndef _NO_NAMESPACE
namespace lib2Desa {
#endif

AEventNotifier::AEventNotifier(void)
{
}

AEventNotifier::~AEventNotifier(void)
{
	events.clear();
	listeners.clear();
}

const shared_ptr<IEvent> AEventNotifier::pollEvent(void)
{
	if(!events.size()) return shared_ptr<IEvent>();

	shared_ptr<IEvent> e = events.front();
	events.pop_front();
	return e;
}

void AEventNotifier::addListener(const shared_ptr<IEventListener>& l)
{
	listeners.push_back(l);
}

void AEventNotifier::removeListener(const shared_ptr<IEventListener>& l)
{
	listeners.remove(l);
}

void AEventNotifier::notifyListeners(void)
{
	if(events.size() == 0) return;

	list<shared_ptr<IEventListener> >::iterator it;
	list<shared_ptr<IEvent> >::iterator it2;

	it = listeners.begin();

	for(; it != listeners.end(); it++)
	{
		it2 = events.begin();
		for(; it2 != events.end(); it2++)
		{
			if(!(*it2)->isConsumed())
			{
				(*it)->notify((*it2));
			}
		}
	}
/*
	it2 = events.begin();
	for(; it2 != events.end(); it2++)
	{
		(*it2) = null;
	}*/

	events.clear();
}

#ifndef _NO_NAMESPACE
}
#endif
